home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 42 / Amiga Format AFCD42 (Issue 126, Aug 1999).iso / -serious- / programming / c / bitmap24 / bitmap24.h < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-17  |  5.1 KB  |  200 lines

  1. #ifndef BITMAP24_H
  2. #define BITMAP24_H
  3. /* -------------------------------------------------------------------------- *\
  4.    BITMAP24.H, 24 bit bitmap handling, including IFF24 saving; header file
  5.    Copyright (C) 1999  Jarno van der Linden
  6.    jarno@kcbbs.gen.nz
  7.  
  8.    This library is free software; you can redistribute it and/or
  9.    modify it under the terms of the GNU Library General Public
  10.    License as published by the Free Software Foundation; either
  11.    version 2 of the License, or (at your option) any later version.
  12.  
  13.    This library is distributed in the hope that it will be useful,
  14.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.    Library General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU Library General Public
  19.    License along with this library; if not, write to the
  20.    Free Software Foundation, Inc., 59 Temple Place - Suite 330, Cambridge,
  21.    MA 02139, USA.
  22.  
  23.  
  24.    01 May 1999: Brought it all together in some sort of distributable form
  25. \* -------------------------------------------------------------------------- */
  26.  
  27. /* -------------------------------- Includes -------------------------------- */
  28. #include <exec/types.h>
  29. #include <dos/dos.h>
  30.  
  31. #include <math.h>
  32.  
  33. /* ------------------------------ Definitions ------------------------------- */
  34. enum {
  35.     BITMAP24_ERROR_NONE = 0,
  36.     BITMAP24_ERROR_NOBITMAP,
  37.     BITMAP24_ERROR_FILEOPEN,
  38.     BITMAP24_ERROR_OUTOFBOUNDS,
  39.     BITMAP24_ERROR_ALLOCFAILURE,
  40.     BITMAP24_ERROR_FILEFORMAT,
  41.     BITMAP24_ERROR_PARSE,
  42.     BITMAP24_ERROR_PREPARSE
  43. };
  44.  
  45. /* --------------------------------- Macros --------------------------------- */
  46.  
  47. /* -------------------------------- Typedefs -------------------------------- */
  48. typedef struct {
  49.     UBYTE r;
  50.     UBYTE g;
  51.     UBYTE b;
  52. } Colour;
  53.  
  54. /* ------------------------------ Proto Types ------------------------------- */
  55.  
  56. /* -------------------------------- Structs --------------------------------- */
  57.  
  58. class BitMap24 {
  59.     public:
  60.         BitMap24();
  61.         BitMap24(char *file);
  62.         virtual ~BitMap24();
  63.  
  64.         int GetError();
  65.         BOOL HasError();
  66.         int ResetError();
  67.         virtual char *GetErrorStr(int error);
  68.         virtual char *GetErrorStr();
  69.  
  70.         UWORD GetWidth();
  71.         UWORD GetHeight();
  72.         UWORD GetRealWidth();
  73.         UWORD GetRealHeight();
  74.  
  75.         virtual void GetColour(Colour *c,int x,int y);
  76.         virtual UBYTE GetRed(int x,int y);
  77.         virtual UBYTE GetGreen(int x,int y);
  78.         virtual UBYTE GetBlue(int x,int y);
  79.         virtual void SetColour(UBYTE r,UBYTE g,UBYTE b,int x,int y);
  80.         virtual void SetColour(const Colour &c, int x,int y);
  81.  
  82.         inline UWORD GetWidthFast();
  83.         inline UWORD GetHeightFast();
  84.         inline UWORD GetRealWidthFast();
  85.         inline UWORD GetRealHeightFast();
  86.  
  87.         virtual inline void GetColourFast(Colour *c,int x,int y);
  88.         virtual inline UBYTE GetRedFast(int x,int y);
  89.         virtual inline UBYTE GetGreenFast(int x,int y);
  90.         virtual inline UBYTE GetBlueFast(int x,int y);
  91.         virtual inline void SetColourFast(UBYTE r,UBYTE g,UBYTE b,int x,int y);
  92.         virtual inline void SetColourFast(const Colour &c, int x,int y);
  93.  
  94.         virtual void WriteBitMap(char *file);
  95.         virtual void ReadBitMap(char *file);
  96.  
  97.         virtual inline UBYTE *GetBitMap();
  98.  
  99.         virtual void SetSize(UWORD width,UWORD height);
  100.         BOOL BoundsCheck(int x,int y);
  101.  
  102.     protected:
  103.         void SetError(int error);
  104.  
  105.     private:
  106.         void Constructor(UWORD width,UWORD height);
  107.         inline UBYTE GatherBits(int row, int c, int b, int x);
  108.         inline void WriteRun(struct IFFHandle *iff, int row, int c, int b, int runstart, int runend);
  109.         inline void WriteDump(struct IFFHandle *iff, int row, int c, int b, int runstart, int runend);
  110.         inline void FindRun(int row, int c, int b, int start, int *runstart, int *runlength);
  111.         inline void SpreadBits(int row, int c, int b, int x, UBYTE v);
  112.         struct IFFHandle *OpenIFFParse(char *file, ULONG chunk);
  113.         void CloseIFFParse(struct IFFHandle *iff);
  114.         inline void WriteChunkBytesCache(struct IFFHandle *iff, APTR data, LONG datasize);
  115.         inline BOOL ReadChunkBytesCache(struct IFFHandle *iff, UBYTE *data, LONG datasize);
  116.     private:
  117.         Colour *bitmap;
  118.         UWORD width,height;
  119.         UWORD realwidth,realheight;
  120.         int bytewidth;
  121.         int error;
  122. };
  123.  
  124.  
  125. /* -------------------------------- Globals --------------------------------- */
  126.  
  127. /* ---------------------------------- Code ---------------------------------- */
  128. inline UWORD BitMap24::GetWidthFast()
  129. {
  130.     return width;
  131. }
  132.  
  133.  
  134. inline UWORD BitMap24::GetHeightFast()
  135. {
  136.     return height;
  137. }
  138.  
  139.  
  140. inline UWORD BitMap24::GetRealWidthFast()
  141. {
  142.     return realwidth;
  143. }
  144.  
  145.  
  146. inline UWORD BitMap24::GetRealHeightFast()
  147. {
  148.     return realheight;
  149. }
  150.  
  151.  
  152. inline void BitMap24::GetColourFast(Colour *c,int x,int y)
  153. {
  154.     *c = bitmap[y*width+x];
  155. }
  156.  
  157.  
  158. inline UBYTE BitMap24::GetRedFast(int x,int y)
  159. {
  160.     return bitmap[y*width+x].r;
  161. }
  162.  
  163.  
  164. inline UBYTE BitMap24::GetGreenFast(int x,int y)
  165. {
  166.     return bitmap[y*width+x].g;
  167. }
  168.  
  169.  
  170. inline UBYTE BitMap24::GetBlueFast(int x,int y)
  171. {
  172.     return bitmap[y*width+x].b;
  173. }
  174.  
  175.  
  176. inline void BitMap24::SetColourFast(UBYTE r,UBYTE g,UBYTE b,int x,int y)
  177. {
  178.     Colour *bp;
  179.  
  180.     bp = &(bitmap[y*width+x]);
  181.     bp->r = r;
  182.     bp->g = g;
  183.     bp->b = b;
  184. }
  185.  
  186.  
  187. inline void BitMap24::SetColourFast(const Colour &c,int x,int y)
  188. {
  189.     bitmap[y*width+x] = c;
  190. }
  191.  
  192.  
  193. inline UBYTE *BitMap24::GetBitMap()
  194. {
  195.     return (UBYTE *)bitmap;
  196. }
  197.  
  198.  
  199. #endif /* BITMAP24_H */
  200.